#include "BoardView.h"

#include <boost/bind.hpp>
#include "../../models/network/CardViewModel.h"
#include "../../../utils/LateEvents.hpp"
#include "../../../domain/configuration/NetworkConfiguration.h"

using namespace Views::Server;
using namespace Utils;

BoardView::BoardView(Domain::Network::GameServer* server, Utils::GUI::Form* form,
	Players::Player* player, Players::Player* current_player)
	: Graphic::BoardView(form), server(server), server_owner(player), current_player(current_player)
{
}

void BoardView::askForPlayerNewPosition(Players::Player*, Cards::RoomsCards rooms_cards)
{
	if (current_player == server_owner)
	{
		Graphic::BoardView::askForPlayerNewPosition(current_player, rooms_cards);
	}
	else
	{
		this->rooms_cards = rooms_cards;
		server->doOnReceiveData(Domain::Configuration::Network::BOARD_ROOM_PICK,
			boost::bind(&BoardView::onButtonClick, this, _1, _2));
	}
}

void BoardView::onButtonClick(Cards::RoomCard* card)
{
	sf::Packet packet;
	Cards::CardsCollection cards(Cards::PlayersCards(), Cards::WeaponsCards(), rooms_cards);
	Views::Models::Network::CardViewModel cvm(card);
	packet << cvm;
	server->sendToAll(Domain::Configuration::Network::BOARD_ROOM_PICK, packet);
	Graphic::BoardView::onButtonClick(card);
}

void BoardView::onButtonClick(Domain::Network::PlayerClient* player_client, sf::Packet packet)
{
	if (player_client->getPlayer() == current_player)
	{
		sf::Packet packet_copy = packet;
		Cards::CardsCollection all_cards(Cards::PlayersCards(), Cards::WeaponsCards(), rooms_cards);
		Views::Models::Network::CardViewModel cvm(all_cards);
		packet >> cvm;
		server->sendToAll(Domain::Configuration::Network::BOARD_ROOM_PICK, packet_copy);
		playerRoomChangeEvent(dynamic_cast<Cards::RoomCard*>(cvm.card)->room);
	}
}

